//
//  MainWC.m
//  sandbox
//
//  Created by 杨岑 on 12/11/2016.
//  Copyright © 2016 杨岑. All rights reserved.
//

#import "MainWC.h"
#import "MyTextView.h"
#import "Test.h"


@interface MainWC ()
@end

@implementation MainWC
{
    NSRange _changedRange;
    NSString* _changedString;
    NSTextAttachmentCell* _cell;
    __weak IBOutlet NSImageView* _imageView;
    __unsafe_unretained IBOutlet MyTextView *_textView;
}

- (void)awakeFromNib {
    NSFont* font = [NSFont userFixedPitchFontOfSize:18];
    [_textView setFont:font];
    NSMutableDictionary* copy = [NSMutableDictionary dictionaryWithDictionary:_textView.typingAttributes];
    copy[NSFontAttributeName] = font;
    _textView.typingAttributes = copy;

    NSAttributedString* s = [self createImageCell:[NSImage imageNamed:NSImageNameUser]];
    [_textView.textStorage setAttributedString:s];
    
    /* The following code sets textview to fixed font only */
    _textView.font = [NSFont userFixedPitchFontOfSize: 15];
    _textView.enabledTextCheckingTypes = 0;
}

- (void)undoReplacingAttributedString:(NSAttributedString*)oldString with:(NSAttributedString*)newString range:(NSRange)range
{
    if (!oldString) oldString = [_textView.textStorage attributedSubstringFromRange:range];
    NSRange newRange = {range.location, newString.length};
    [[_textView.undoManager prepareWithInvocationTarget:self] undoReplacingAttributedString:newString with:oldString range:newRange];
    //[self.undoManager setActionName:@"Parsed"];
    [_textView.textStorage replaceCharactersInRange:range withAttributedString:newString];
}

- (void)textDidChange:(NSNotification *)notification
{
    NSLog(@"%s: %ld,%ld %ld,%ld", __PRETTY_FUNCTION__, _changedRange.location, _changedRange.length,
          _textView.textStorage.length,
          _textView.string.length);

#ifdef PASTE_METHOD_DELEGATE
    if (_textView.pasted && _changedString.length)
    {
        _textView.pasted = NO;
        NSMutableAttributedString* newText = [[NSMutableAttributedString alloc] initWithString:_textView.pastedText attributes:@{NSForegroundColorAttributeName: [NSColor blueColor]}];
        [newText deleteCharactersInRange:NSMakeRange(newText.length / 2, newText.length - newText.length / 2)];
        [newText addAttribute:NSFontAttributeName value:_textView.font range:NSMakeRange(0, newText.length)];
        NSRange range = {_changedRange.location, _changedString.length};
        [self undoReplacingAttributedString:nil with:newText range:range];
    }
#endif
}

- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
    _changedRange = affectedCharRange;
    _changedString = replacementString;
    /*NSLog(@"%s: %ld,%ld %ld,%ld: %@", __PRETTY_FUNCTION__, 
            _changedRange.location, _changedRange.length,
            _textView.textStorage.length,
            _textView.string.length,
            replacementString);*/
    return YES;
}

- (void)textViewDidChangeSelection:(NSNotification *)notification
{
    NSLog(@"%s: %ld,%ld", __PRETTY_FUNCTION__, _textView.selectedRange.location, _textView.selectedRange.length);
}

- (void)undoReplacingText:(NSAttributedString*)oldText with:(NSAttributedString*)newText range:(NSRange)range
{
    if (!oldText) oldText = [_textView.textStorage attributedSubstringFromRange:range];
    NSRange newRange = {range.location, newText.length};
    [[_textView.undoManager prepareWithInvocationTarget:self] undoReplacingText:newText with:oldText range:newRange];
    //[self.undoManager setActionName:@"Parsed"];
    [_textView.textStorage replaceCharactersInRange:range withAttributedString:newText];
}

- (NSAttributedString*)createImageCell:(NSImage*)image {
    _cell = [[NSTextAttachmentCell alloc] init];
    _cell.image = image;
    NSTextAttachment* attachment = [[NSTextAttachment alloc] init];
    attachment.attachmentCell = _cell;

    return [NSAttributedString attributedStringWithAttachment:attachment];
}

- (IBAction)test_click:(id)sender
{
    NSLog(@"%@", [NSPasteboard generalPasteboard].types);

    _cell.image = [NSImage imageNamed:NSImageNameComputer];
    [_textView.layoutManager invalidateLayoutForCharacterRange:NSMakeRange(0, 1) actualCharacterRange:nil];

    NSRange range = {0, _textView.string.length};
    [_textView.textStorage removeAttribute:NSFontAttributeName range:range];
}

@end
